/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gitee.fdc.web.page;

import com.gitee.fdc.kits.HttpKit;
import com.gitee.fdc.kits.ToolUtil;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author qs
 */
public class PageKit {

    public static PageReq getPageReqs() {
        HttpServletRequest request = HttpKit.getRequest();
        String ls = request.getParameter("limit");
        int limit = 0;
        if (!ToolUtil.isEmpty(ls)) {
            limit = Integer.valueOf(ls);
        }
        String os = request.getParameter("offset");
        int offset = 10;
        if (!ToolUtil.isEmpty(ls)) {
            offset = Integer.valueOf(request.getParameter("offset"));
        }
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        PageReq pageReq = new PageReq(limit, offset, sort, order);
        if (ToolUtil.isEmpty(sort)) {
            pageReq.setOpenSort(false);
        } else {
            pageReq.setOpenSort(true);
            if ("asc".equalsIgnoreCase(order)) {
                pageReq.setAsc(true);
            } else {
                pageReq.setAsc(false);
            }
        }
        return pageReq;
    }

    public static <T> PageInfoBT packForBT(List<T> page, long total) {
        PageInfoBT p = new PageInfoBT();
        p.setRows(page);
        p.setTotal(total);
        return p;
    }

    public static <T> PageInfoBT packForBT(List<T> pList) {
        PageInfoBT p = new PageInfoBT();
        p.setRows(pList);
//        if (pList instanceof com.github.pagehelper.Page) {
//            p.setTotal(((com.github.pagehelper.Page) pList).getTotal());
//        } else {
//             p.setTotal(pList.size());
//        }

        try {
            p.setTotal(((com.github.pagehelper.Page) pList).getTotal());
        } catch (Exception e) {
//            e.printStackTrace();
            p.setTotal(pList.size());
        }
        return p;
    }
}
